package com.niiwoo.civet.trade.service.local.third;

import com.niiwoo.civet.trade.constant.RabbitConstant;
import com.niiwoo.civet.trade.dao.entity.Project;
import com.niiwoo.civet.trade.dao.entity.ProjectBorrowerPlanNew;
import com.niiwoo.civet.trade.dao.mapper.ProjectBorrowerPlanNewMapperExt;
import com.niiwoo.civet.trade.dao.mapper.ProjectMapperExt;
import com.niiwoo.civet.trade.dto.FollowWorkTaskAddDTO;
import com.niiwoo.civet.trade.dto.common.TradeBatchTransOrderDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by yuanguang on 2018/1/10.
 */
@Service
@Slf4j
public class AegisLocalService {
    @Autowired
    private ProjectMapperExt projectMapperExt;
    @Autowired
    private ProjectBorrowerPlanNewMapperExt projectBorrowerPlanMapperExt;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 逾期还款通知神盾后台
     * @param dtoSrc
     */
    public void sendOverDueMqMsgToApp(TradeBatchTransOrderDTO dtoSrc){
        try {
            Project project = projectMapperExt.selectByPrimaryKey(dtoSrc.getProjectId());
            log.info("app通知followWorkTaskAdd发送,projectId={}", project.getProjectId());
            ProjectBorrowerPlanNew projectBorrowerPlan = projectBorrowerPlanMapperExt.selectByProjectAndPeriod(dtoSrc.getProjectId(),dtoSrc.getCurrentPeriod());
            FollowWorkTaskAddDTO followWorkTaskAddDTO = new FollowWorkTaskAddDTO();
            followWorkTaskAddDTO.setProjectId(project.getProjectId());
            followWorkTaskAddDTO.setBorrowerMobile(project.getBorrowMobile());
            followWorkTaskAddDTO.setBorrowerPlanId(projectBorrowerPlan.getId());
            followWorkTaskAddDTO.setBorrowerUserId(projectBorrowerPlan.getUserId());
            followWorkTaskAddDTO.setBorrowerUserName(project.getBorrowUserName());
            followWorkTaskAddDTO.setRefundDate(projectBorrowerPlan.getRefundDate());
            followWorkTaskAddDTO.setApplyTime(project.getCreateTime());
            rabbitTemplate.convertAndSend(RabbitConstant.Exchange.FOLLOW_WORK_TASK_ADD, RabbitConstant.RoutingKey.FOLLOW_WORK_TASK_ADD,
                    followWorkTaskAddDTO);
        }catch (Exception e){
            log.error("send to app followWorkTaskAdd failed.",e);
        }
    }
}
